<template>
  <div class="cart">
    <div class="cart-container">
      <h1 class="cart-title">购物车</h1>
      
      <div v-if="!userStore.isLoggedIn" class="login-required">
        <div class="login-required-card">
          <div class="login-icon">🔒</div>
          <h2>请先登录</h2>
          <p>您需要登录后才能查看和管理购物车</p>
          <button class="login-btn" @click="goToLogin">
            立即登录
          </button>
        </div>
      </div>
      
      <div v-else-if="cartItems.length > 0" class="cart-content">
        <div class="cart-items">
          <CartItem 
            v-for="item in cartItems" 
            :key="item.id" 
            :item="item"
          />
        </div>
        
        <div class="cart-summary">
          <div class="summary-card">
            <h3>订单摘要</h3>
            <div class="summary-item">
              <span>商品数量：</span>
              <span>{{ totalItems }} 件</span>
            </div>
            <div class="summary-item">
              <span>商品总价：</span>
              <span class="price">¥{{ totalPrice.toLocaleString() }}</span>
            </div>
            <div class="summary-item">
              <span>运费：</span>
              <span class="price">免费</span>
            </div>
            <hr class="divider">
            <div class="summary-item total">
              <span>总计：</span>
              <span class="price">¥{{ totalPrice.toLocaleString() }}</span>
            </div>
            
            <div class="checkout-actions">
              <button class="checkout-btn" @click="checkout">
                立即结算
              </button>
              <button class="clear-btn" @click="clearCart">
                清空购物车
              </button>
            </div>
          </div>
        </div>
      </div>
      
      <div v-else class="empty-cart">
        <div class="empty-icon">🛒</div>
        <h2>购物车为空</h2>
        <p>去挑选一些心仪的商品吧！</p>
        <button class="shop-now-btn" @click="goShopping">
          立即购物
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import CartItem from '../components/CartItem.vue'
import { useCartStore } from '../stores/cart'
import { useUserStore } from '../stores/user'

const router = useRouter()
const cartStore = useCartStore()
const userStore = useUserStore()

const cartItems = computed(() => cartStore.items)
const totalItems = computed(() => cartStore.totalItems)
const totalPrice = computed(() => cartStore.totalPrice)

const goShopping = () => {
  router.push('/')
}

const goToLogin = () => {
  router.push('/user')
}

const clearCart = () => {
  if (confirm('确定要清空购物车吗？')) {
    cartStore.clearCart()
  }
}

const checkout = () => {
  if (!userStore.isLoggedIn) {
    alert('请先登录后再进行结算')
    router.push('/user')
    return
  }
  
  // 模拟支付流程
  alert(`订单总金额：¥${totalPrice.value.toLocaleString()}\n\n模拟支付成功！\n感谢您的购买！`)
  cartStore.clearCart()
  router.push('/')
}
</script>

<style scoped>
.cart {
  min-height: 100vh;
  background-color: #f8f9fa;
}

.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.cart-title {
  font-size: 2.5rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 40px;
  color: #333;
}

.cart-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 40px;
  align-items: start;
}

.cart-items {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.summary-card {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 20px;
}

.summary-card h3 {
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 1rem;
}

.summary-item.total {
  font-size: 1.2rem;
  font-weight: bold;
}

.price {
  color: #e74c3c;
  font-weight: 600;
}

.divider {
  border: none;
  border-top: 1px solid #eee;
  margin: 16px 0;
}

.checkout-actions {
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.checkout-btn {
  width: 100%;
  padding: 16px;
  background-color: #28a745;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.checkout-btn:hover {
  background-color: #218838;
}

.clear-btn {
  width: 100%;
  padding: 12px;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.2s ease;
}

.clear-btn:hover {
  background-color: #c82333;
}

.empty-cart {
  text-align: center;
  padding: 80px 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 24px;
}

.empty-cart h2 {
  font-size: 2rem;
  color: #666;
  margin-bottom: 16px;
}

.empty-cart p {
  font-size: 1.1rem;
  color: #888;
  margin-bottom: 32px;
}

.shop-now-btn {
  padding: 16px 32px;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.shop-now-btn:hover {
  background-color: #005c99;
}

.login-required {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 400px;
}

.login-required-card {
  text-align: center;
  background: white;
  border-radius: 12px;
  padding: 60px 40px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 100%;
}

.login-icon {
  font-size: 3rem;
  margin-bottom: 24px;
}

.login-required-card h2 {
  font-size: 1.8rem;
  color: #666;
  margin-bottom: 16px;
}

.login-required-card p {
  color: #888;
  margin-bottom: 32px;
  line-height: 1.5;
}

.login-btn {
  padding: 14px 32px;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.login-btn:hover {
  background-color: #005c99;
}

@media (max-width: 768px) {
  .cart-content {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  
  .cart-title {
    font-size: 2rem;
  }
  
  .summary-card {
    position: static;
  }
}
</style> 